import { arrayCommon, lsCommon } from "@whzhuke/common"

export const CityCodeSplit = (v: string): string[] => {
  const isProvince = v.length === 2 ? true : v.endsWith("0000")
  const isCity = isProvince ? false : v.length === 4 ? true : v.endsWith("0000")
  const isArea = !isProvince && !isCity && v.length === 6
  if (!isArea && !isProvince && !isCity) {
    console.error("错误的区域Code")
    return []
  }
  const level = isProvince ? 1 : isCity ? 2 : isArea ? 3 : 1
  return new Array(level).fill("#").reduce((prev, current, index) => {
    const code = v.slice(0, 2 * index + 2)
    prev.push(code)
    return prev
  }, [])
}

export const CityRender = (v: string) => {
  const codeData = CityCodeSplit(v)
  const CityData = arrayCommon.doAlone(lsCommon.getItem("cityData")!, "children")
  return codeData.map(code => CityData.find(m => m.code === code)?.name)
}
